package org.swim.servlet;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.swim.session.RegistrationManagerRemote;


/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");       
		HttpSession session = request.getSession();
		session.setAttribute("page", "register");
		request.getRequestDispatcher("WEB-INF/Register.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
		RequestDispatcher disp = null;
		boolean blockRegistration = false;
		String inputName = (String) request.getParameter("inputName");
		String inputSurname = (String) request.getParameter("inputSurname");
		String inputEmail = (String) request.getParameter("inputEmail");
		String inputPassword = (String) request.getParameter("inputPassword");
		String inputRepeatPassword = (String) request.getParameter("inputRepeatPassword");
		request.setAttribute("name", inputName);
		request.setAttribute("surname", inputSurname);
		request.setAttribute("email", inputEmail);
		request.setAttribute("password", inputPassword);
		request.setAttribute("repeatedPassword", inputRepeatPassword);
		if (inputName.equals("")) {
			request.setAttribute("nameStatus", "noNameInserted");
			blockRegistration = true;
		} else {
			request.setAttribute("nameStatus", "ok");
		}
		if (inputSurname.equals("")) {
			request.setAttribute("surnameStatus", "noSurnameInserted");
			blockRegistration = true;
		} else {
			request.setAttribute("surnameStatus", "ok");
		}
		if (inputEmail.equals("")) {
			request.setAttribute("emailStatus", "noEmailInserted");
			blockRegistration = true;
		} else if (!(inputEmail.contains("@") || inputEmail.contains("."))) {
			request.setAttribute("emailStatus", "wrongEmailInsterted");
			blockRegistration = true;
		} else {
			request.setAttribute("emailStatus", "ok");
		}
		if (inputPassword.equals("")) {
			request.setAttribute("passwordStatus", "noPasswordInserted");
			blockRegistration = true;
		} else {
			request.setAttribute("passwordStatus", "ok");
		}		
		if (inputRepeatPassword.equals("")) {
			request.setAttribute("repeatedPasswordStatus", "noRepeatPasswordInserted");
			blockRegistration = true;
		} else {
			request.setAttribute("repeatedPasswordStatus", "ok");
		}
		if (!inputPassword.equals(inputRepeatPassword)) {
			request.setAttribute("passwordStatus", "passwordNotMatch");
		   	blockRegistration = true;
		}  
		InitialContext ic;
		try {
			ic = new InitialContext();
			RegistrationManagerRemote registrationManager = (RegistrationManagerRemote)ic.lookup("RegistrationManagerRemote");
			if(registrationManager.verifyRegistrationData(inputEmail)== false)
				{
				blockRegistration = true;
				request.setAttribute("emailStatus", "alreadyInserted");
				}
			if (blockRegistration == false){
				registrationManager.createNewUser(inputName, inputSurname, inputEmail, inputPassword);
				request.setAttribute("registrationStatus", "registered");
				disp = request.getRequestDispatcher("WEB-INF/RegistrationSuccess.jsp");
				} else {
				request.setAttribute("registrationStatus", "error");
				disp = request.getRequestDispatcher("WEB-INF/Register.jsp");
			}
		} catch (NamingException e) {
			e.printStackTrace();
		}
		disp.forward(request,response);	
	}

}
